package com.dajie.pt.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.dajie.common.util.StringUtil;
import com.dajie.pt.dao.InvitationApplyDao;
import com.dajie.pt.entity.base.EntityDao;
import com.dajie.pt.model.InvitationApply;
import com.dajie.pt.service.InvitationApplyService;
import com.dajie.pt.service.base.BaseServiceImpl;

@Component
public class InvitationApplyServiceImpl extends BaseServiceImpl<InvitationApply, Integer> implements
		InvitationApplyService<InvitationApply, Integer>
{

	@Autowired
	InvitationApplyDao invitationApplyDao;

	@Override
	public boolean saveInvitationApply(InvitationApply invitationInfo)
	{
		invitationInfo.setResumeInfo("");
		String json = com.alibaba.fastjson.JSONArray.toJSONString(invitationInfo);
		invitationInfo.setResumeInfo(json);
		int saveResult = (Integer) this.invitationApplyDao.save(invitationInfo);
		return saveResult > 0 ? true : false;
	}

	@Override
	public boolean replaceInvitationApply(InvitationApply invitationInfo)
	{
		invitationInfo.setResumeInfo("");
		String json = com.alibaba.fastjson.JSONArray.toJSONString(invitationInfo);
		invitationInfo.setResumeInfo(json);
		int saveResult = (Integer) this.invitationApplyDao.replace(invitationInfo);
		return saveResult > 0 ? true : false;
	}

	@Override
	public int saveInvitationApplyAsPV(InvitationApply invitationInfo)
	{
		InvitationApply infoFromDB = this.invitationApplyDao.getInvitationApplyInfoByUEJ(invitationInfo.getUid(),
				invitationInfo.getEmail(), invitationInfo.getJid());
		if(infoFromDB != null)
		{
			if((infoFromDB.getStatus() == 2 || infoFromDB.getStatus() == 3))
			{
				return -1;
			}else
			{
				invitationInfo.setStatus(1);
				invitationInfo.setResumeInfo("");
				String json = com.alibaba.fastjson.JSONArray.toJSONString(invitationInfo);
				invitationInfo.setResumeInfo(json);
				return (Integer)this.invitationApplyDao.updateByUniqueKey(invitationInfo);
			}
		}else
		{
			invitationInfo.setStatus(1);
			invitationInfo.setResumeInfo("");
			String json = com.alibaba.fastjson.JSONArray.toJSONString(invitationInfo);
			invitationInfo.setResumeInfo(json);
			return (Integer)this.invitationApplyDao.save(invitationInfo);
		}
	}

	
	@Override
	public int saveInvitationApplyAsAccept(InvitationApply iaInfo)
	{
		InvitationApply infoFromDB = this.invitationApplyDao.getInvitationApplyInfoByMobileAndUid(iaInfo.getMobile(),iaInfo.getUid());
		if(infoFromDB != null)
		{
			if((infoFromDB.getStatus() == 2 || infoFromDB.getStatus() == 3))
			{
				return -1;
			}else
			{
				iaInfo.setStatus(1);
				iaInfo.setResumeInfo("");
				String json = com.alibaba.fastjson.JSONArray.toJSONString(iaInfo);
				iaInfo.setResumeInfo(json);
				return this.invitationApplyDao.updateByUniqueKey(iaInfo);
			}
		}else
		{
			iaInfo.setStatus(1);
			iaInfo.setResumeInfo("");
			String json = com.alibaba.fastjson.JSONArray.toJSONString(iaInfo);
			iaInfo.setResumeInfo(json);
			return (Integer)this.invitationApplyDao.save(iaInfo);
		}
	}
	
	@Override
	public int saveInvitationApplyAsSubmit(InvitationApply invitationInfo)
	{
		invitationInfo.setStatus(2);
		invitationInfo.setResumeInfo("");
		String json = com.alibaba.fastjson.JSONArray.toJSONString(invitationInfo);
		invitationInfo.setResumeInfo(json);
		return (Integer) this.invitationApplyDao.updateByUniqueKey(invitationInfo);
	}
	
	@Override
	public int saveInvitationApplyAsIgnore(InvitationApply invitationInfo)
	{
		invitationInfo.setStatus(3);
		invitationInfo.setResumeInfo("");
		String json = com.alibaba.fastjson.JSONArray.toJSONString(invitationInfo);
		//String jsons = com.alibaba.fastjson.JSONObject.toJSONString(invitationInfo);
		invitationInfo.setResumeInfo(json);
		return (Integer) this.invitationApplyDao.updateByUniqueKey(invitationInfo);
	}
	
	@Override
	public EntityDao getEntityDao()
	{
		return this.invitationApplyDao;
	}

	@Override
	public InvitationApply getInvitationApplyInfoByUEJ(Integer uid, String email, String jid)
	{

		return this.invitationApplyDao.getInvitationApplyInfoByUEJ(uid, email, jid);
	}
	
	@Override
	public List<InvitationApply> findByPage(Map<String,Object> params)
	{
		List<InvitationApply> list = new ArrayList<InvitationApply>();
		List<InvitationApply> res = this.find(params);
		for(InvitationApply data : res)
		{
			if(StringUtil.isNotEmpty(data.getResumeInfo()))
			{
				//InvitationApply ia =com.alibaba.fastjson.JSONObject.toJavaObject(com.alibaba.fastjson.JSONObject.parseObject(data.getResumeInfo()), InvitationApply.class);
				InvitationApply ia = json2InvitationApply(data);
				list.add(ia);
			}
		}
		return list;
	}

	private InvitationApply json2InvitationApply(InvitationApply data)
	{
		if(data == null)
		{
			return data;
		}
		InvitationApply ia = com.alibaba.fastjson.JSONArray.toJavaObject(com.alibaba.fastjson.JSONObject.parseObject(data.getResumeInfo()), InvitationApply.class);
		if(ia != null)
		{
			ia.setId(data.getId());
			ia.setCreateDate(data.getCreateDate());
			ia.setUpdateDate(data.getUpdateDate());
		}
		return ia;
	}
	
	@Override
	public InvitationApply getByMobile(String mobile)
	{
		List<InvitationApply> list = this.invitationApplyDao.findByMap(map("mobile",mobile));
		if(list != null && list.size() > 0)
		{
			return this.json2InvitationApply(list.get(0));
		}else
		{
			return null;
		}
	}

	@Override
	public void saveOrUpdate(InvitationApply entity)
	{
		entity.setResumeInfo("");
		entity.setResumeInfo(com.alibaba.fastjson.JSONArray.toJSONString(entity));
		this.invitationApplyDao.saveOrUpdate(entity);
	}
	
	@Override
	public void saveSubmitResume(InvitationApply entity)
	{
		entity.setResumeInfo("");
		entity.setResumeInfo(com.alibaba.fastjson.JSONArray.toJSONString(entity));
		this.invitationApplyDao.saveOrUpdate(entity);
		this.invitationApplyDao.saveApplyRecord(entity);
	}
	
	@Override
	public void saveIgnore(InvitationApply entity)
	{
		this.invitationApplyDao.saveApplyRecord(entity);
	}
	
	@Override
	public Object save(InvitationApply entity)
	{
		entity.setResumeInfo("");
		entity.setResumeInfo(com.alibaba.fastjson.JSONArray.toJSONString(entity));
		return this.invitationApplyDao.save(entity);
	}
	
	@Override
	public InvitationApply findUniqueBy(final Object... params)
	{
		return json2InvitationApply((InvitationApply) getEntityDao().findUniqueBy(params));
	}
	
	@Override
	public String getIdsByMobile(String mobile)
	{
		
		return this.invitationApplyDao.getIdsByMobile(mobile);
	}
	
	@Override
	public List<InvitationApply> showlist(Map<String,Object> params)
	{
		
		List<InvitationApply> list = new ArrayList<InvitationApply>();
		List<InvitationApply> res = this.invitationApplyDao.showlist(params);
		for(InvitationApply data : res)
		{
			if(StringUtil.isNotEmpty(data.getResumeInfo()))
			{
				//InvitationApply ia =com.alibaba.fastjson.JSONObject.toJavaObject(com.alibaba.fastjson.JSONObject.parseObject(data.getResumeInfo()), InvitationApply.class);
				InvitationApply ia = json2InvitationApply(data);
				list.add(ia);
			}
		}
		
		return list;
	}
	
	public void saveCode(String mobile,String code)
	{
		this.invitationApplyDao.saveCode(map("mobile",mobile,"code",code));
	}
	
	public String getCode(String mobile)
	{
		Object code = this.invitationApplyDao.getCode(mobile);
		return code != null ? (String)code : null;
	}
}
